﻿
namespace FolderSync
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Security;
    using System.Threading;
    using System.Diagnostics;


    public class Enqueuer
    {
        public IFileFilter filter { get; set; }
        public Syncer Syncer { get; set; }
        public bool WatchOnly { get; set; }
        public INotifier Notifier { get; set; }
        public string LogTag { get; set; }
        // Define the event handlers.
        public void OnChanged(object source, FileSystemEventArgs e)
        {
            OnChanged(source, e, 0);
        }
        public void OnChanged(object source, FileSystemEventArgs e, int ErrorCount)
        {
            var translater = source as ITranslater;
            var rawEvent = new FileEvent(translater.TranslatePath, e) { LogTag = LogTag, };
            rawEvent.ErrorCount = ErrorCount;
            if (!filter.Match(rawEvent.FolderName, rawEvent.FileName))
            {
                Notifier.Message("ENQ {0}: {1} {2}", LogTag, "Rejected", rawEvent);
                return;
            }
            Notifier.Message("ENQ {0}: {1} {2}, Target is:{3}", LogTag, (WatchOnly ? "Watching" : "Enqueuing"), rawEvent, rawEvent.TranslatePath(rawEvent.FullPath));
            if (!WatchOnly) Syncer.Enqueue(rawEvent, ErrorCount);
        }
        public void OnRenamed(object source, RenamedEventArgs e)
        {
            OnChanged(source, e);
        }
    }
}